package com.tarena.lbs.user.service.impl;

import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.po.UserTagsPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


import java.util.Date;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRepository userRepository;
    public LoginVO login(UserLoginParam param) throws BusinessException {
        log.debug("登录参数业务:{}",param);
        UserPO userPO = userRepository.login(param);
        if (userPO==null){
            throw new BusinessException(PassportResultEnum.ADMIN_NONE_EXIST);
        }
        if (!param.getPassword().equals(userPO.getPassword())){
            throw  new BusinessException(PassportResultEnum.ADMIN_PASSWORD_ERROR);
        }
        LoginVO loginVO = new LoginVO();
        loginVO.setId(userPO.getId());
        loginVO.setNickname(userPO.getNickName());
        log.debug("业务参数:"+ userPO);
        return loginVO;
        }
    public List<UserVO> list(@RequestBody UserQuery userQuery) {
        log.info("业务层："+userQuery);
        List<UserVO> list = userMapper.list(userQuery);
        return list;
    }

    @Override
    public UserVO detail(Integer id) {
        UserVO userVO = userRepository.detail(id);
        return userVO;
    }

    @Override
    public void save(UserParam param) throws BusinessException {
        UserVO userVO = userRepository.selectByPhone(param.getPhone());
        if (userVO!=null){
            throw new BusinessException(PassportResultEnum.USER_EXIST);
        }
        if (userVO==null){
            UserPO userPO = new UserPO();
            BeanUtils.copyProperties(param,userPO);
            userPO.setRegTime(new Date());
            userPO.setStatus(0);
            userPO.setUnionId(String.valueOf(Roles.USER));
            userRepository.save(userPO);
        }

    }

    @Override
    public PageResult<UserVO> List(UserQuery userQuery) {
        PageResult<UserVO> pageResult = userRepository.selectPageList(userQuery);
        return pageResult;
    }

}
